package 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.utils.getDefinitionByName;
	import flash.external.ExternalInterface;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author amyflash.com
	 */
	public class Preloader extends MovieClip 
	{
		
		public function Preloader() 
		{
			if (!checkDomain())
			return;
			
			if (stage) {
				stage.scaleMode = StageScaleMode.NO_SCALE;
				stage.align = StageAlign.TOP_LEFT;
			}
			
			addEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// TODO show loader
		}
		
		private function ioError(e:IOErrorEvent):void 
		{
			trace(e.text);
		}
		
		private function progress(e:ProgressEvent):void 
		{
			// TODO update loader
		}
		
		private function checkFrame(e:Event):void 
		{
			if (currentFrame == totalFrames) 
			{
				stop();
				loadingFinished();
			}
		}
		
		private function loadingFinished():void 
		{
			removeEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// TODO hide loader
			
			startup();
		}
		
		private function startup():void 
		{
			var mainClass:Class = getDefinitionByName("Main") as Class;
			addChild(new mainClass() as DisplayObject);
		}
		
		//sitelock
		private function checkDomain():Boolean
		{
		  var url:String = loaderInfo.loaderURL;
		  var weburl:String;
		  try 
		  {
			weburl = ExternalInterface.call("function getUrl(){return window.location.href;}");
		  }
		  catch (e:Error)
		  {
			weburl = "";
		  }
		  // 允许运行的域名列表，使用|分隔
		  var allowedDomains:String = ".*amyflash.com|.*dev.local|localhost";
		  var allowedPattern:String = "^http(|s)://("+allowedDomains+")/";
		  var domainCheck:RegExp = new RegExp(allowedPattern, "i");
		  
		  if (weburl != "")
		  {
			if (!domainCheck.test(url) && !domainCheck.test(weburl))
			{
			  showInfo();
			  return false;
			}
		  }
		  else
		  {
			showInfo();
			return false;
		  }
		  return true;
		}

		private function showInfo():void
		{
		  var textWarning:TextField = new TextField();
		  textWarning.defaultTextFormat = new TextFormat("Tahoma", 24, 0x333333);
		  textWarning.selectable = false;
		  textWarning.multiline = true;
		  textWarning.autoSize = "center";
		  textWarning.x = 300;
		  textWarning.y = 100;
		  addChild(textWarning);
		  
		  textWarning.htmlText = '<p align="center"><br /><br />请在这个地址访问:<a target="_blank" href="http://www.amyflash.com"><font color="#CC0000">www.amyflash.com</font></a>.</p>';
		}
		
	}
	
}